﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class UpException : System.Web.UI.Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["userName"] == null || Session["roleID"] == null)
            {
                Response.Redirect("Login.aspx");
                Response.End();
            }
            lblWriter.Text =BasicClass.GetTrueNameByUserName(Session["userName"].ToString());
            BindToDdlDep();
            BindToDdlName();
            btnReset.Attributes.Add("onclick", "location.href='UpException.aspx';");
            txtWriteDate.Value = DateTime.Now.ToShortDateString();
            if (Session["roleID"].ToString() != "1")
            {
                chbAddOldYC.Visible = false;
            }
            else
            {
                chbAddOldYC.Visible = true;
            }
        }

    }
    //绑定数据到DdlDep控件
    protected void BindToDdlDep()
    {
        DBOperation DBO = new DBOperation();
        string sqlString = "select DEPID,DEPNAME from DEPARTMENT where STATUS=1 order by DEPID ASC";
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        ddlToDep.DataSource = sdr;
        ddlToDep.DataTextField = "DEPNAME";
        ddlToDep.DataValueField = "DEPID";
        ddlToDep.SelectedValue = "1";
        ddlToDep.DataBind();
        sdr.Close();

    }
    //绑定数据到DdlName控件
    protected void BindToDdlName()
    {
        DBOperation DBO = new DBOperation();
        string sqlString = "select TRUENAME,USERNAME from USERS where STATUS=1 and USERNAME<>'admin' and DEPID=" + ddlToDep.SelectedValue + " order by DEPID ASC";
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        ddlToUserName.DataSource = sdr;
        ddlToUserName.DataTextField = "TRUENAME";
        ddlToUserName.DataValueField = "USERNAME";
        ddlToUserName.DataBind();
        sdr.Close();

    }
    //实现部门与人员的二级联动    
    protected void ddlToDep_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindToDdlName();
    }
   
    protected void btnUpFile_Click(object sender, EventArgs e)
    {
        
    }
    protected Boolean UpAttachFiles()
    {
        lblFailMsg.Text = "<br/>";
        bool success = false;        
        DataOperation DBO = new DataOperation();
        string FilePath = DBO.CheckDirectory("YCBG");
        bool fileCheck = true;
        string errorMsg="以下文件服务器中存在同名文件：";
        for (int j = 0; j < Request.Files.Count; j++)
        { 
            string filePath = Request.Files[j].FileName;
            //判断上传文件框里是否有文件
            if (filePath != "")
            {
                string shortFileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
                string DataFilePath = FilePath + "\\" + shortFileName;
                if (File.Exists(Server.MapPath("files") + DataFilePath))
                {
                    lblFailMsg.Text = lblFailMsg.Text + "<font color='red'>存在同名文件：" + shortFileName + "</font>;请更名后再添加该文件。<br/>";
                    errorMsg += shortFileName + ",";
                    fileCheck = false;
                    success = false;
                }

            }
        }
        if (fileCheck)
        {
            success = true;
            for (int i = 0; i < Request.Files.Count; i++)
            {
                string filePath = Request.Files[i].FileName;
                //判断上传文件框里是否有文件
                if (filePath != "")
                {
                    string shortFileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
                    string DataFilePath = FilePath + "\\" + shortFileName;
                    if (File.Exists(Server.MapPath("files") + DataFilePath))
                    {
                        lblFailMsg.Text = lblFailMsg.Text + "<font color='red'>存在同名文件：" + shortFileName + "</font>;请更名后再添加该文件。<br/>";
                        success = false;
                    }
                    else
                    {
                        //将文件名记录到动态数组，然后上传到服务器                    
                        Request.Files[i].SaveAs(Server.MapPath("files") + DataFilePath);
                        txtFilePath.Text += DataFilePath + ",";
                        lblFileName.Text += shortFileName + ",";
                        int fileCount = Convert.ToInt32(lblFileCount.Text.ToString().Trim());
                        fileCount += 1;
                        lblFileCount.Text = fileCount.ToString();
                    }
                }
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('"+errorMsg+"');", true);  
        }
        return success;
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!UpAttachFiles())
        {
            lblAlert.Text = "<font color='red'>上传的文件中有与服务器上的文件重名，请改名后再上传！</font>";            
        }
        else
        {
            YCDeal addYC = new YCDeal();
            addYC.UserName = Session["userName"].ToString();
            addYC.Title = BasicClass.HtmlEncode(txtTitle.Text);
            addYC.DEPID = Convert.ToInt32(ddlToDep.SelectedValue.ToString().Trim());
            addYC.Content = Request.Form["txtaContent"];
            addYC.CurrentOperater = ddlToUserName.SelectedValue;
            addYC.AddDate =Convert.ToDateTime(txtWriteDate.Value);
            addYC.Files = txtFilePath.Text.ToString().Trim();
            if (chbAddOldYC.Checked)
            {
                addYC.Status = "1";
            }
            else
            {
                addYC.Status = "0";
            }
            lblAlert.Text = "";
            if (addYC.AddYC())
            {
                Response.Write("<script language='javascript'>alert('异常提交成功！');location.href='UpException.aspx';</script>");
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {

    }
}
